package com.jiaai.cloud.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.live.entity.TAnchor;
import com.jiaai.cloud.live.entity.TAnchorTask;
import com.jiaai.cloud.live.entity.TLiveBroadcast;
import com.jiaai.cloud.live.service.ITAnchorService;
import com.jiaai.cloud.live.service.ITAnchorTaskService;
import com.jiaai.cloud.live.service.ITBroadcastTaskService;
import com.jiaai.cloud.live.service.ITLiveBroadcastService;
import com.jiaai.cloud.live.service.impl.TAnchorServiceImpl;
import com.jiaai.cloud.utils.HttpHeaderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author 佳爱网络科技
 * @version V1.0
 * @title TaskAPI
 * @since 2017/8/17
 */

@RestController
@RequestMapping("/api/v1/task")
public class TaskAPI {
 @Autowired
 private ITAnchorTaskService taskService;
 @Autowired
 private ITBroadcastTaskService broadcastTaskService;
 @Autowired
 private ITLiveBroadcastService broadcastService;
@Autowired
 private ITAnchorService anchorService;

@GetMapping("/findTaskByAnchor/{anchorId}")
 public Object findTaskByAnchorId(@PathVariable("anchorId")String anchorId){
    return taskService.findTaskByAnchorId(anchorId);
 }
    /**
     * 添加任务
     * @param task
     * @return
     * @throws ServiceException
     */
 @PostMapping("/add")
 public Object addTask(@RequestBody TAnchorTask task)throws ServiceException{
        taskService.insert(task);
        return RestResultGenerator.genSuccessResult();
 }

    /**
     * 删除任务
     * @param taskId
     * @return
     * @throws ServiceException
     */
 @GetMapping("/del/{taskId}")
 public  Object deleteTask(@PathVariable("taskId") Long taskId)throws  ServiceException{
        taskService.deleteById(taskId);
        return RestResultGenerator.genSuccessResult();
 }

    /**
     * 发布任务
     * @param taskId
     * @return
     * @throws ServiceException
     */
 @GetMapping("/release/{taskId}")
 public  Object release(@PathVariable("taskId") Long taskId)throws  ServiceException{
     TAnchorTask task=null;
     try {
          task=taskService.selectById(taskId);
     }catch (Exception e){
        throw  new ServiceException("任务查询失败",e.getMessage());
     }

     try {
        TAnchor anchor=anchorService.selectOne(new EntityWrapper().eq("userId", HttpHeaderUtils.getUserId()));
        task.setAnchorId(anchor.getAnchorId());
     }catch (Exception e){
         throw  new ServiceException("无权限",e.getMessage());
     }

     task.setStatus(2);
     taskService.insertOrUpdate(task);
    return  RestResultGenerator.genSuccessResult();
 }
}
